﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Cryptography;

namespace shuatibao.Tool
{
    public class Utils
    {
        #region 时间戳转换
        public static long ConvertDateTimeInt(System.DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
            TimeSpan ts = (time - startTime);

            return (long)ts.TotalSeconds;
        }


        public static string ConvertDateTimeInterval(long timestamp) {
            DateTime result = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
            return result.ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion


        #region JSON抛出
        public static string toJSON(Object item)
        {
            JsonSerializer serializer = new JsonSerializer();
            StringWriter sw = new StringWriter();
            serializer.Serialize(new JsonTextWriter(sw), item);
            string info = sw.GetStringBuilder().ToString();
          
            return info;
        }


        public static string jsonBackManager(HttpContext contextWithBase, Object item,bool noback)
        {
            JsonSerializer serializer = new JsonSerializer();
            StringWriter sw = new StringWriter();
            serializer.Serialize(new JsonTextWriter(sw), item);
            string info = sw.GetStringBuilder().ToString();
            if (noback != true) {
                contextWithBase.Response.Write(sw.GetStringBuilder().ToString());
            }
            
            return info;
        }

        public static void jsonBackManager( HttpContext contextWithBase, string infoStr)
        {
            contextWithBase.Response.Write(infoStr);
        }




        #region MD5
        public static string MD5Manager(string input)
        {
            input = "app_id12345678date2021-10-18 12:00:00methodanalyze.taowordtaoword(x1fPXtcPydw)xxxxxxx";
            using (MD5 md5Hash = MD5.Create())
            {
                // 计算输入字符串的MD5哈希值
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 将字节数组转换为十六进制字符串
                StringBuilder sBuilder = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回大写形式的哈希字符串
                return sBuilder.ToString().ToUpper();
            }
        }


        public static string MD5Manager(byte[] byteData)
        {
            // 创建MD5对象
            using (MD5 md5 = MD5.Create())
            {
                // 计算输入数据的哈希值
                byte[] hashBytes = md5.ComputeHash(byteData);

                // 将字节数组转换为十六进制字符串
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        #endregion

        #endregion



        private static readonly Random Random = new Random();
        private const string AllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        public static string RandomString(int length)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int index = Random.Next(AllowedChars.Length);
                char c = AllowedChars[index];
                sb.Append(c);
            }
            return sb.ToString();
        }

    }



}